﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ExpenseSystem.Model;
using ExpenseSystem.Fether;
using ExpenseSystem.DataUpdater;
using ExpenseSystem.Common;
using ExpenseSystem.Core.Authority;
using ExpenseSystem.DataUpdater.Validation;

namespace ExpenseSystem.Core.SalesPointModules
{
    public class SalesPointUpdaterRegister
    {
        int rows;
        UserProfile userProfile;
        public SalesPointUpdaterRegister()
        {
            userProfile = Authority.Authority.GetUserProfile();
        }
        public NormalDataUpdater<SalesPointModel> GetSalesPointUpdater()
        {
            NormalDataUpdater<SalesPointModel> systemDataUpdater = new NormalDataUpdater<SalesPointModel>();
            systemDataUpdater.DataUpdating += DataNotSpaceValid;
            systemDataUpdater.DataUpdating += SingletonValid;
            systemDataUpdater.DataUpdating += RegionValid;
            systemDataUpdater.DataDeleting += CheckAvaliable;
            return systemDataUpdater;
        }
        private void CheckAvaliable(object sender, DataValidationEventArgs<SalesPointModel> e)
        {
            CustomerFetcher customerFetcher = new CustomerFetcher();
            OrderFetcher orderFetcher = new OrderFetcher();
            SalesPointEmployeeFetcher salesPointEmployeeFetcher = new SalesPointEmployeeFetcher();
            string message;
            if(salesPointEmployeeFetcher.ListByMainId(e.Delete.Site,e.Delete.AutoId,out message,out rows).Count>0)
            {
                e.Cancle = true;
                message = "请先把本门店所有成员移除";
                return;
            }
            if(orderFetcher.ListBySalesPointId(e.Delete.Site,e.Delete.AutoId,out message, out rows).Count>0)
            {
                e.Cancle = true;
                message = "本门店有对应的订单信息";
                return;

            }
            if(customerFetcher.ListBySalesPoint(e.Delete.Site,e.Delete.AutoId,out message, out rows).Count>0)
            {
                e.Cancle = true;
                message = "本门店有对应的客户信息";
                return;
            }

        }

        private void SingletonValid(object sender, DataValidationEventArgs<SalesPointModel> e)
        {
            SalesPointFetcher SalesPointFetcher = new SalesPointFetcher();
            string message = "";
             
            SalesPointModel SalesPoint = SalesPointFetcher.GetByCode(userProfile.SiteId,e.Insert.Code,out message);
            if (SalesPoint != null && SalesPoint.AutoId != e.Insert.AutoId)
            {
                e.Cancle = true;
                message = String.Format("代号[{0}]已被[{1}]占用", SalesPoint.Code, SalesPoint.Name);
                return;
            }
        }

        private void DataNotSpaceValid(object sender, DataValidationEventArgs<SalesPointModel> e)
        {
            if (String.IsNullOrWhiteSpace(e.Insert.Code))
            {
                e.Message = "代号不能为空";
                e.Cancle = true;
                return;
            }
            if (String.IsNullOrWhiteSpace(e.Insert.Name))
            {
                e.Message = "名称不能为空";
                e.Cancle = true;
                return;
            }
        }

        private void RegionValid(object sender, DataValidationEventArgs<SalesPointModel> e)
        {
            string message;
            RegionFetcher regionFetcher = new RegionFetcher();
            if(regionFetcher.Get(e.Insert.Site,e.Insert.RegionId,out message)==null)
            {
                e.Message = "对应的区域不存在";
                e.Cancle = true;
                return;
            }
        }
    }
}
